package cn.javadog.ss.spring.tx;

import javax.sql.DataSource;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.jdbc.datasource.DriverManagerDataSource;
import org.springframework.transaction.annotation.EnableTransactionManagement;

/**
 * @author 余勇
 * @date 2019年11月25日 18:32:00
 */
@Configuration
@EnableTransactionManagement
public class JdbcConfig {

	@Bean
	public DataSource jdbcDataSource() {
		final String URL = "jdbc:mysql://localhost:3306/mysql_learn?useSSL=true";
		final String DRIVER = "com.mysql.cj.jdbc.Driver";
		final String USERNAME = "root";
		final String PASSWORD = "root";

		DriverManagerDataSource dataSource = new DriverManagerDataSource(URL,USERNAME,PASSWORD);
		dataSource.setDriverClassName(DRIVER);

		return dataSource;
	}

	@Bean
	public JdbcTemplate jdbcTemplate(DataSource dataSource){
		return new JdbcTemplate(dataSource);
	}

	@Bean
	public JdbcDao jdbcDao(JdbcTemplate jdbcTemplate){
		return new JdbcDao(jdbcTemplate);
	}

	@Bean
	public DataSourceTransactionManager dataSourceTransactionManager(DataSource dataSource){
		return new DataSourceTransactionManager(dataSource);
	}

}
